import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';

let adminMenus=JSON.parse(localStorage.getItem('adminMenus')+"");
if(adminMenus==null){
    adminMenus=[];
}


const routeList: { path: any; name: any; component: () => Promise<any>; meta: { title: any; }; }[] = [];
adminMenus.forEach((item:any) => {
    routeList.push({
        path: item.path,
        name: item.code,
        component: () => import(`../views/${item.component}`),
        meta: {
            title: item.name
        }
    });
});


const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/Body.vue'),
    meta: {
      title: '登录'
    }
  },
  {
    path: '/manage',
    name: 'manage',
    component: () => import('../views/manage/Body.vue'),
    // 子菜单
    children: [
      {
        path: '/manage/index',
        name: 'manageIndex',
        component: () => import('../views/manage/index/Body.vue'),
        meta: {
          title: '首页'
        }
      },
      ...routeList
    ]
  },
]


const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})


router.beforeEach((to, from, next) => {
  document.title = to.meta.title+"";
  next();
})

export default router
